package com.spark.check

import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentStatePagerAdapter
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.spark.check.model.TaskModel
import com.spark.common.BaseFragment
import com.spark.common.Const
import com.spark.common.ext.wrapIO
import com.spark.common.net.Http
import kotlinx.android.synthetic.main.frag_task_info.*
import kotlinx.android.synthetic.main.frag_task_info_child.*

/**
 * 0分任务
 * 全部任务
 * 普通任务
 * 各种任务
 */
class TaskInfoFragment : BaseFragment() {

    override fun contentResId(): Int {
        return R.layout.frag_task_info
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        iv_back.setOnClickListener { pop() }
        val tmp = mutableListOf("全部(12)", "1部分(6)", "2部分(1)", "3部分(2)", "4部分(5)")
        tmp.forEach {
            tabs.addTab(tabs.newTab().setText(it))
        }
        activity?.apply {
            val taskPagerAdapter = TaskPagerAdapter(supportFragmentManager, tmp)
            pager.adapter = taskPagerAdapter
            tabs.setupWithViewPager(pager)
        }
    }


}

class TaskPagerAdapter(fm: FragmentManager, var tabs: MutableList<String>) : FragmentStatePagerAdapter(fm) {
    override fun getCount(): Int {
        return tabs.size
    }

    override fun getItem(position: Int): Fragment {
        return TaskInfoChildFragment.newInstance(position)
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return tabs[position]
    }

}

/**
 * Task info child <<<<<<>>>>>>>
 */
class TaskInfoChildFragment : BaseFragment() {
    private var index = 0
    private lateinit var taskAdapter: TasksAdpater

    companion object {
        fun newInstance(index: Int): TaskInfoChildFragment {
            val fragment = TaskInfoChildFragment()
            val args = Bundle()
            args.putInt(Const.tag_index, index)
            fragment.arguments = args
            return fragment
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_task_info_child
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        index = arguments?.getInt(Const.tag_index) ?: 0
        taskAdapter = TasksAdpater(R.layout.item_task)
        recy_tasks.adapter = taskAdapter
        recy_tasks.layoutManager = LinearLayoutManager(context)

        taskAdapter.addData(mutableListOf(TaskModel.mock(), TaskModel.mock(), TaskModel.mock(), TaskModel.mock(), TaskModel.mock(), TaskModel.mock()))
    }

    /**
     * 获取任务详情
     */
    private fun fetchTaskInfo() {
        val params = mutableMapOf(
                Pair(Const.qr_taskid, "id"),
                Pair(Const.qr_type, "$index")
        )
        activity?.apply {
            Http.request()
                    .rxPost("${Const.base_url}${Const.act_stdchk_list}", params)
                    .wrapIO()
                    .subscribe(
                            {},
                            {
                                it.printStackTrace()
                                hideLoader()
                            },
                            {
                                hideLoader()
                            },
                            {
                                dispose = it
                                loading()
                            }
                    )
        }
    }
}